import numpy as np
import pandas as pd


def clean(data_csv, replace_xlsx):
    data_dirty = pd.read_csv(data_csv, names=['date', 'name', 'location', 'data1', 'data2', 'data3'], header=None,
                             sep='\t')
    # 去空值
    data = data_dirty.dropna()
    # 去字段
    data = data[data['name'] != '水位']

    # 标准化_小数定标,因为数据呈现很大或很小
    def decimal_scaling(data):
        max_abs = np.max(np.abs(data))
        scaled_data = data / (10 ** np.ceil(np.log10(max_abs)))
        return scaled_data

    data[['data1', 'data2', 'data3']] = decimal_scaling(data[['data1', 'data2', 'data3']])
    # 修改name编码
    re = pd.read_excel(replace_xlsx)
    mapping1 = dict(zip(re['name'], re['n']))
    data['name'] = data['name'].apply(lambda x: mapping1[x])
    # 修改location编码
    mapping = dict(zip(re['location'], re['re']))
    data['location'] = data['location'].map(mapping)
    # 修改date字段的类型为int
    data['date'].astype(str)
    data['date'] = data['date'].apply(lambda x: x.replace('/', ''))
    data['date'] = data['date'].apply(lambda x: x.replace(' ', ''))
    data['date'] = data['date'].astype(int)
    # result
    GNSS = list(data.groupby(['name']))[0][1]
    # 环境温湿度
    ev_humidity = list(data.groupby(['name']))[5][1]
    # 索力
    tension = list(data.groupby(['name']))[6][1]
    # 风速风向
    wind = list(data.groupby(['name']))[7][1]
    # target
    # 加速度
    acceleration = list(data.groupby(['name']))[1][1]
    # 敦梁相对位移（水平位移）
    h_Rdisplacement = list(data.groupby(['name']))[2][1]
    # 应变
    d_shape = list(data.groupby(['name']))[3][1]
    # 挠度（垂直位移）
    v_Rdisplacement = list(data.groupby(['name']))[4][1]
    return [GNSS, ev_humidity, tension, wind, acceleration, h_Rdisplacement, d_shape, v_Rdisplacement]
